/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.github.oldtoys.springboot.hazelcast.session;

import com.hazelcast.core.HazelcastInstance;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 *
 * @author Administrator
 */
@Controller
public class WebController {

    private static final Logger LOGGER = LoggerFactory.getLogger(WebController.class);

    /**
     * Each time the "{@code /}" URL is called, increment the hit counter and
     * indicate that the "{@code index.html}" page should be returned.
     *
     * @param httpSession The current session
     * @return The view to render, in MVC terms.
     */
    @RequestMapping(value = "/")
    public String index(HttpSession httpSession, ModelMap mmap) {
        Integer hits = (Integer) httpSession.getAttribute("hits");

        LOGGER.info("index() called, hits was '{}', session id '{}'", hits, httpSession.getId());

        if (hits == null) {
            hits = 0;
        }
        Object so = SpringContextUtil.getBean("hazelcastInstance");
        if (so != null) {
            mmap.put("hasHzIns", true);
            HazelcastInstance hso = (HazelcastInstance) so;
            mmap.put("hzInsName", hso.toString());

        } else {
            mmap.put("hasHzIns", false);
        }
        httpSession.setAttribute("hits", ++hits);

        return "index";
    }
}
